﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Motoball.Paddles
{
    public class PaddleAnimation
    {
        protected float innerRadius;
        protected float wheelRadius = 80;
        protected float rotationsPerRadian;
        public float wheelRotation = 0.0f;
        protected float rotation = 0.0f;

        protected ModelBone leftWheel;
        protected ModelBone rightWheel;

        protected Matrix leftWheelTransform;
        protected Matrix rightWheelTransform;

        public PaddleAnimation()
        { }

        public virtual void Update(GameTime gameTime, float rotation)
        {
            wheelRotation = (rotation * rotationsPerRadian) % 360.0f; 
        }
#if !XBOX
        protected SaveGame save = new SaveGame();
#endif
        public virtual void Initialize(float innerRadius, Model model)
        {
#if !XBOX
            save.itemCount = model.Bones.Count;
            save.items = new string[save.itemCount];
            for (int i = 0; i < save.itemCount; i++)
            {
                save.items[i] = model.Bones[i].Name;
            }
            Motoball.savegame.Save(save);
#endif
            this.innerRadius = innerRadius;
            CalcRotPerRadian();
        }
        public virtual void UpdateAnimation(GameTime gameTime)
        {
        }
        public virtual void Draw(GameTime gameTime)
        {
            leftWheel.Transform = Matrix.CreateRotationY(wheelRotation) * leftWheelTransform;
            rightWheel.Transform = Matrix.CreateRotationY(wheelRotation) * rightWheelTransform;
        }

        public virtual void Reset(float startRotation)
        {
            wheelRotation = (startRotation * rotationsPerRadian) % 360.0f;
        }
        protected void CalcRotPerRadian()
        {
            float circleCircumference = MathHelper.TwoPi * innerRadius;
            float wheelCircumference = MathHelper.TwoPi * wheelRadius;
            rotationsPerRadian = circleCircumference / wheelCircumference;
        }
        public virtual void UpdateColor(Vector3 color, BasicModel basicModel)
        { 
        }
        protected static Vector3[] exhaustKeyFrames;
        public static void InitExhaustKeyFrames()
        {
            exhaustKeyFrames = new Vector3[10];
            exhaustKeyFrames[0] = new Vector3(-3, 0, 2.5f);
            exhaustKeyFrames[1] = new Vector3(2.45f, 0, 2.0f);
            exhaustKeyFrames[1] = new Vector3(8.45f, 0, 8.0f);
            exhaustKeyFrames[2] = new Vector3(-0.001f, 0, 0.003f);
            exhaustKeyFrames[3] = new Vector3(3, 0, -3f);
            exhaustKeyFrames[4] = new Vector3(-2.44f, 0, -2.113f);
            exhaustKeyFrames[5] = new Vector3(7f, 0, -6.4f);
            exhaustKeyFrames[6] = new Vector3(2.315f, 0, 1.234f);
            exhaustKeyFrames[7] = new Vector3(-1.224f, 0, 1.44f);
            exhaustKeyFrames[8] = new Vector3(-2.632f, 0, 1.873f);
            exhaustKeyFrames[9] = new Vector3(-10f, 0, 2.5f);
            
        }

    }

    public class BoneProperties
    {
        public ModelBone modelBone;
        public Matrix sourceTransform;

        public BoneProperties(ModelBone bone)
        {
            modelBone = bone;
            sourceTransform = bone.Transform;
        }
    }
}
